package org.jfk.core.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import com.jfinal.kit.PathKit;

public class SqlFileUtils {
	public static String readSqlFromFile(String fileName) {
		try {
			String filePath = PathKit.getRootClassPath();
			filePath = filePath + "/sql/";
			filePath = filePath + fileName;

			String encoding = "UTF-8";
			File file = new File(filePath);
			String sqlStr = "";
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					sqlStr = sqlStr + lineTxt;
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
			System.out.println(sqlStr);
			return sqlStr;
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		readSqlFromFile("sys.getAllUrls.sql");
	}	
}
